package com.yingxin.yxerp.module.print.controller;

import com.yingxin.yx.framework.user.core.IUserSupport;
import com.yingxin.yx.framework.user.dto.UserDto;
import com.yingxin.yxerp.module.print.entity.dto.PrintInfo;
import com.yingxin.yxerp.module.print.mapper.PrintRecordMapper;
import com.yingxin.yxerp.module.print.service.IPrintRecordService;
import com.yingxin.yxerp.module.print.vo.req.PrintBatchReqVo;
import com.yingxin.yxerp.module.print.vo.req.PrintReqVo;
import com.yingxin.yxerp.module.print.vo.resp.PrintRecordRespVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;

/**
 * 打印记录 前端控制器
 * @author fanchao
 * @since 2022-02-19
 */

@RestController
public class PrintController {

    @Autowired
    private IPrintRecordService printRecordService;

    @SuppressWarnings("all")
    @Autowired
    private PrintRecordMapper printRecordMapper;

    @Autowired
    private IUserSupport userSupport;

    @ApiOperation(value = "打印人信息")
    @PostMapping("/print/userInfo")
    public PrintRecordRespVo info() {
        UserDto user = userSupport.getUser();
        PrintRecordRespVo record = new PrintRecordRespVo();
        record.setPrinterId(user.getUserId());
        record.setPrinterName(user.getStaffName());
        record.setPrintTime(new Date());
        return record;
    }

    @ApiOperation(value = "打印检查")
    @PostMapping("/print/check")
    public PrintInfo checkPrint(@RequestBody @Valid PrintReqVo req) {
        return printRecordService.checkPrint(req.getBusinessId(), req.getBusinessNo(), req.getBusinessType());
    }

    @ApiOperation(value = "打印")
    @PostMapping("/print")
    public void print(@RequestBody @Valid PrintReqVo req) {
        printRecordService.print(req.getBusinessId(), req.getBusinessNo(), req.getBusinessType());
    }

    @ApiOperation(value = "打印")
    @PostMapping("/printBatch")
    public void printBatch(@RequestBody @Valid PrintBatchReqVo req) {
        for (PrintReqVo info : req.getInfos()) {
            printRecordService.print(info.getBusinessId(), info.getBusinessNo(), info.getBusinessType());
        }
    }

    @ApiOperation(value = "税务打印")
    @PostMapping("/print/tax")
    public void printTax(@RequestBody @Valid PrintReqVo req) {
        printRecordService.printTax(req.getBusinessId(), req.getBusinessNo(), req.getBusinessType());
    }

}